import Vue from 'vue'
import Router, { RouteConfig, RouterOptions } from 'vue-router'
import Layout from '../Layout/index.vue'
import Framework from '../views/Framework.vue'

Vue.use(Router)
const contexts = require.context('./modules', false, /\.ts$/)

export const constantRoutes: RouteConfig[] = [
  // 用于刷新
  {
    path: '/',
    name: 'home',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('../views/portal/admin.vue'),
        name: 'portal',
        meta: {
          affix: true,
          hidden: true,
          title: 'portal'
        }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path*',
        component: () => import(/* webpackChunkName: "redirect" */ '@/views/redirect/index.vue')
      }
    ]
  },
  /*  {
    path: '/layout',
    name: 'layout',
    component: Layout
  }, */
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/index.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/mgr/editInfo',
    component: Layout,
    children: [{
      path: '/mgr/editInfo',
      name: '修改信息',
      component: Framework,
      meta: {
        title: '修改信息'
      }
    }]
  },
  {
    path: '/*',
    component: Layout,
    children: [{
      path: '',
      name: 'framework',
      component: Framework,
      meta: {
        // title: '...',
        hidden: true
      }
    }]
  }
]

export const asyncRoutes: RouteConfig[] = []

contexts.keys().forEach((key: string) => {
  const route = contexts(key).default
  route.children.forEach((child: RouteConfig) => {
    child.component = Framework
  })
  asyncRoutes.push(route)
})

// 获取到home地址
export const getHomeRoute = () => {
  // asyncRoutes.
}

/* export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRoutes
}) */

const createRouter = () => new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: (to, from, savedPosition) => {
    return {
      x: 0,
      y: 0
    }
  },
  routes: constantRoutes
})

const index = createRouter()

export function resetRouter() {
  const newRouter = createRouter();
  (index as any).matcher = (newRouter as any).matcher // reset index
}

export default index
